Practice Problems on Functions in C

Posted on December 18, 2023 by Vishesh Namdev
Python C C++ Java
C Programming Language

Basic Practice Problems based on chapter which you learn in previous Tutorials.

1. Prime number using Interval using Function

#include <stdio.h>Copy Code
// Function to check if a number is prime
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

// Function to print prime numbers in an interval
void printPrimesInInterval(int start, int end) {
printf("Prime numbers in the interval [%d, %d]: ", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
}

int main() {
int start, end;

// Input the interval
printf("Enter the start of the interval: ");
scanf("%d", &start);
printf("Enter the end of the interval: ");
scanf("%d", &end);

// Call the function to print prime numbers in the interval
printPrimesInInterval(start, end);

return 0;
}

This program defines a function isPrime to check whether a given number is prime or not. Then, it defines another function printPrimesInInterval to print prime numbers within a specified interval.

2. Sum of Natural number using Recursion

#include <stdio.h>Copy Code
// Function to calculate the sum of natural numbers using recursion
int sumOfNaturalNumbers(int n) {
if (n == 0) {
return 0;
} else {
return n + sumOfNaturalNumbers(n - 1);
}
}

int main() {
int n;

// Input the value of n
printf("Enter a positive integer n: ");
scanf("%d", &n);

// Check if the entered value is non-negative
if (n < 0) {
printf("Please enter a non-negative integer.\n");
} else {
// Call the function to calculate the sum and display the result
int sum = sumOfNaturalNumbers(n);
printf("Sum of first %d natural numbers is %d.\n", n, sum);
}

return 0;
}

3. Binary to Decimal using Function

#include <stdio.h>Copy Code
// Function to convert binary to decimal
int binaryToDecimal(long long binaryNumber) {
int decimalNumber = 0, i = 0, remainder;

while (binaryNumber != 0) {
remainder = binaryNumber % 10;
binaryNumber /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}

return decimalNumber;
}

int main() {
long long binaryNumber;

// Input the binary number
printf("Enter a binary number: ");
scanf("%lld", &binaryNumber);

// Call the function to convert and display the result
int decimalNumber = binaryToDecimal(binaryNumber);
printf("Decimal equivalent: %d\n", decimalNumber);

return 0;
}

4. GCD using Recursion

gcd() is a recursive function. It has two parameters i.e. a and b. If b is greater than 0, then a is returned to the main() function. Otherwise, the gcd() function recursively calls itself with the values b and a%b.

#include <stdio.h>Copy Code
// Function to calculate GCD using recursion
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}

int main() {
int num1, num2;

// Input the two numbers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

// Call the function to calculate GCD and display the result
int result = gcd(num1, num2);
printf("GCD of %d and %d is %d\n", num1, num2, result);

return 0;
}